﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class EventBase : MonoBehaviour,IEditable<EventBase>
    {
        public bool isEditing;

        public EventType type;
        
        public int noteSoundID;
        public BoxCollider box;
        public SpriteRenderer thisRenderer;
        public SpriteRenderer splitLine;
        public JudgementLine judgeline;
        public EventData eventData;
        public TextMesh dataShow;

        [Serializable]
        public class EventData
        {
            public double time;
            public double duration;
            public int beatTime;
            public int beatDuration;
            public int easeType;
            public Z1029.NoteGlobalView.ValueSet value;
            public int index;
        }

        public virtual void Awake()
        {
            //thisRenderer = GetComponent<SpriteRenderer>();
            judgeline = LevelController.instance.judgelines[0];
        }

        public virtual void Start()
        {
            eventData.beatTime = Utils.GetBeatTimeFromTime(eventData.time, judgeline.bpm);
            eventData.beatDuration = Utils.GetBeatTimeFromTime(eventData.duration, judgeline.bpm);
            thisRenderer.color = (Color.blue + Color.white) / 2;
            dataShow.GetComponent<Renderer>().sortingLayerID = splitLine.sortingLayerID;//Note
            dataShow.GetComponent<Renderer>().sortingOrder = 1000;//Effect
            transform.localEulerAngles = Vector3.zero;
            SetNotePosition();
            ChangeData();
            if (eventData.value.start == eventData.value.end && eventData.value.start2 == eventData.value.end2)
                Destroy(gameObject);
        }

        public virtual float CalculateNotePosY()
        {
            float num = CalculateDistance();
            float realY = transform.localPosition.y + (num - transform.localPosition.y);
            return realY;
        }

        public virtual float CalculateDistance()
        {
            return (float)(judgeline.GetSpeedDistance(eventData.time));
        }

        public virtual void SetNotePosition()
        {
            transform.localPosition = new Vector3(0, CalculateNotePosY(), 0f);
        }

        public virtual void Update()
        {
            SetHoldPosition();
            bool flag = !(LevelController.nowTime > eventData.time + eventData.duration) && transform.localPosition.x < 10;
            thisRenderer.enabled = transform.position.y < 20 && eventData.time + eventData.duration < LevelController.nowTime;
            if (!flag)return;
            splitLine.enabled = flag;
            dataShow.gameObject.active = flag;
            box.enabled = flag;
            UpdateHoldSize();
            SetDirection();
        }

        public void ChangeData()
        {
            eventData.time = Utils.GetTimeFromBeatTime(eventData.beatTime, judgeline.bpm);
            eventData.duration = Utils.GetTimeFromBeatTime(eventData.beatDuration, judgeline.bpm);
            if (type == EventType.Speed)
            {
                dataShow.text = eventData.value.start.ToString();
                thisRenderer.enabled = false;
            }
            else if (type == EventType.Move) dataShow.text = string.Format("({0:0.0},{2:0.0})-({1:0.0},{3:0.0})", eventData.value.start, eventData.value.end, eventData.value.start2, eventData.value.end2);
            else dataShow.text = string.Format("{0:0.0}- {1:0.0}", eventData.value.start, eventData.value.end);
        }

        public void UpdateHoldSize()
        {
            float sizeY;
            eventData.time = Utils.GetTimeFromBeatTime(eventData.beatTime, judgeline.bpm);
            eventData.duration = Utils.GetTimeFromBeatTime(eventData.beatDuration, judgeline.bpm);
            sizeY = Mathf.Abs((float)(judgeline.GetSpeedDistance(eventData.time + eventData.duration) - judgeline.GetSpeedDistance(eventData.time)));
            if (judgeline.GetSpeedDistance(eventData.time) <= 0)
            {
                sizeY = Mathf.Abs((float)judgeline.GetSpeedDistance(eventData.time + eventData.duration));
            }
            sizeY *= 100;
            thisRenderer. transform.localScale = new Vector3(200f, sizeY, 1f);
        }

        public void SetDirection()
        {
        }
        public void SetHoldPosition()
        {
            if (judgeline.GetSpeedDistance(eventData.time) > 0) transform.localPosition = new Vector3(0, CalculateNotePosY(), 0f);
            else transform.localPosition = new Vector3(0, 0, 0f);
            //if (judgeline.GetSpeedDistance(noteData.time) > 0) transform.localPosition = new Vector3(noteData.positionX, (float)(noteData.time-LevelController.nowTime)*noteData.speed, 0f);
        }

        public static bool HLNote(int beatTime)
        {
            int count = 0;
            for (int i = 0; i < LevelController.instance.notes.Length; i++)
            {
                if (LevelController.instance.notes[i].noteData.beatTime == beatTime)
                {
                    count++;
                }
            }
            return count >= 2;
        }

        public T GetData<T>()
        {
            throw new NotImplementedException();
        }

        public void SetData<T>()
        {
            throw new NotImplementedException();
        }

        public void UpdateData()
        {
            throw new NotImplementedException();
        }
    }

    public enum EventType
    {
        Speed,Move,Rotate,Alpha
    }
}